package fy.com.controller.information;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import fy.com.util.Const;

import fy.com.util.AppUtil;


import fy.com.util.PageData;

import fy.com.controller.base.BaseController;
import fy.com.entity.Page;
import fy.com.service.information.InformationService;

@Controller
@RequestMapping(value="/information")
public class InformationController extends BaseController{
	@Resource(name="informationService")
	private InformationService informationService;
	
	
	
	
	
	@RequestMapping("/test")
	public void testRequest (HttpServletRequest request )
	{
		String name = request.getParameter("name") ;
		System.out.println(name);
		
	}
	
	/**
	 * 新增
	 */
	@RequestMapping(value="/save")
	public ModelAndView save()throws Exception{
		ModelAndView mv =this.getModelAndView();
		PageData pd= new PageData();
		pd=this.getPageData();
		informationService.save(pd);
		mv.addObject("msg", "success");
		System.out.println("新增了一条记录！");
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete")
	public void delete(PrintWriter out){
		PageData pd=new PageData();
		try {
			pd=this.getPageData();
			informationService.delete(pd);
			System.out.println("删除了一条记录！");
			out.write("success");
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除
	 */
//	@RequestMapping(value="/delete")
//	public ModelAndView delete() throws Exception{
//		logBefore(logger, "删除information");
//		ModelAndView mv =this.getModelAndView();
//		PageData pd=new PageData();
//		pd=this.getPageData();
//		informationService.delete(pd);
//		System.out.println("删除了一条记录！");
//		mv.setViewName("information_list");
//		return mv;
//	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit()throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		informationService.edit(pd);
		mv.addObject("msg","success");
		System.out.println("修改了一条记录！");
		mv.setViewName("save_result");
		return mv;
	}
	
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try {
			pd=this.getPageData();
			List<PageData> varList = informationService.list(page);
			mv.setViewName("information/information_list");
			mv.addObject("varList",varList);
			mv.addObject("pd",pd);
			mv.addObject(Const.SESSION_QX,this.getHC());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	
	
	/**
	 * 去新增页面
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		ModelAndView mv = this.getModelAndView();
		PageData pd =new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("information/information_edit");
			mv.addObject("msg","save");
			mv.addObject("pd",pd);
		} catch (Exception e){
			e.printStackTrace();
		}
		return mv;
	}
	
	/**
	 * 去修改页面
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView geEdit(){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = informationService.findById(pd);
			mv.setViewName("information/information_edit");
			mv.addObject("msg","edit");
			mv.addObject("pd",pd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		PageData pd = new PageData();		
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				informationService.deleteAll(ArrayDATA_IDS);
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return AppUtil.returnObject(pd, map);
	}
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */

}
